package com.clean.cleantotal.utils

import android.Manifest
import android.animation.ValueAnimator
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.animation.doOnEnd
import com.clean.cleantotal.BuildConfig
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.bean.HomeBean
import com.clean.cleantotal.bean.JunkBean
import com.clean.cleantotal.bean.JunkGroup
import com.clean.cleantotal.bean.TikBean
import com.clean.cleantotal.pop.BackPop
import com.clean.cleantotal.pop.PopScan
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.interfaces.SimpleCallback

object Utils {

    // 选择清理的文件
    var cleanJunFiles = ArrayList<JunkBean>()

    // 垃圾分组
    val cleanGroups = ArrayList<JunkGroup>()

    // 空文件选择的清理数据
    var selectEmptyList = ArrayList<JunkBean>()

    // 选择的三方应用清理的数据
    var selectTikList = ArrayList<TikBean>()

    fun openPolicy(context: Context) {
        // todo 修改正式隐私协议地址
        val url = if (BuildConfig.DEBUG) {
            "https://www.google.com"
        } else {
            "https://www.google.com"
        }
        val uri = Uri.parse(url)
        val intent = Intent(Intent.ACTION_VIEW, uri)
        val list = context.packageManager.queryIntentActivities(intent, 0)
        if (list.isNotEmpty()) {
            context.startActivity(intent)
        }
    }


    fun requestPr(context: Context, callBack: () -> Unit) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            XXPermissions.with(context).permission(
                listOf(
                    Manifest.permission.MANAGE_EXTERNAL_STORAGE
                )
            ).request(object : OnPermissionCallback {
                override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                    callBack.invoke()
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    super.onDenied(permissions, doNotAskAgain)
                    callBack.invoke()
                }
            })
        } else {
            XXPermissions.with(context).permission(
                Permission.WRITE_EXTERNAL_STORAGE
            ).request(object : OnPermissionCallback {
                override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                    callBack.invoke()
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    super.onDenied(permissions, doNotAskAgain)
                    callBack.invoke()
                }
            })
        }
    }

    fun showAnimalProcess(
        process: TextView, clean: Boolean = false, callBack: () -> Unit
    ): ValueAnimator {
        val anim = ValueAnimator.ofInt(0, 100)
        anim.duration = 12000
        anim.addUpdateListener {
            process.text = "${it.animatedValue as Int}%"
//            if (clean) {
//            } else {
//                process.text = "${it.animatedValue as Int}%"
//            }
        }
        anim.doOnEnd {
            callBack.invoke()
        }
        anim.start()
        return anim
    }

    private var popScan: BasePopupView? = null
    fun showScanDialog(
        context: Context,
        funType: FunType?,
        loadingLayout: ConstraintLayout,
        callBack: (animal: ValueAnimator) -> Unit,
        finish: () -> Unit
    ) {
        popScan?.dismiss()
        popScan = XPopup.Builder(context).dismissOnTouchOutside(false).dismissOnBackPressed(false)
            .setPopupCallback(object : SimpleCallback() {
                override fun onShow(popupView: BasePopupView?) {
                    super.onShow(popupView)
                    loadingLayout.visibility = View.INVISIBLE
                }
            }).asCustom(PopScan(context, funType, callBack, finish)).show()
    }

    private var backPop: BasePopupView? = null
    fun showBackPop(context: Context, size: String, out: () -> Unit, next: () -> Unit) {
        backPop?.dismiss()
        backPop = XPopup.Builder(context).popupAnimation(PopupAnimation.NoAnimation)
            .dismissOnTouchOutside(false).dismissOnBackPressed(false)
            .asCustom(BackPop(context, size, out, next)).show()
    }

    fun updateIndex(list: List<HomeBean>): List<HomeBean> {
        if (list.isNotEmpty()) {
            if (list.size == 1) {
                list.first().bgIndex = 1
            } else if (list.size >= 2) {
                list.first().bgIndex = 0
                list.last().bgIndex = -1
            }
        }
        return list
    }
}